package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.constant.BspsAppParamConstant;
import cc.rengu.igas.bsps.common.dao.BspsMchntBindInfoMapper;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.enums.BindStatusEnum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.*;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户绑定.
 * Updated by zhangxuran on 2020/04/02 --新增收银员解绑方法
 */
public class BspsMchntBindInfoMapperImpl implements BspsMchntBindInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);

    @Override
    public List<MchntBindInfo> selectBspsMchntBindInfoListByUserId(String instId, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBindInfoListByUserId");
        MchntBindInfo mchntBindInfo = new MchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setBindUserId(userId);
        mchntBindInfo.setBindStatus(BindStatusEnum.BIND.getStatus());

        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBindInfo, MchntBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据用户标识<{}>获取商户绑定信息失败!", userId);
            return null;
        }
        return objectList.stream().map(item -> (MchntBindInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 根据角色查询商户绑定信息
     *
     * @param instId  机构标识
     * @param mchntNo 用户标识
     * @return 商户绑定信息
     * @throws Exception 异常
     */
    @Override
    public List<MchntBindInfo> selectBspsMchntBindInfoByMchntNo(String instId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBindInfoByMchntNo");
        MchntBindInfo mchntBindInfo = new MchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setMchntNo(mchntNo);
        mchntBindInfo.setBindStatus(BindStatusEnum.BIND.getStatus()); //绑定
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBindInfo, MchntBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号：<{}>获取商户绑定信息为空!", mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBindInfo) (item)).collect(Collectors.toList());
    }

    /**
     * 根据角色查询商户绑定信息
     *
     * @param instId   机构标识
     * @param mchntNo  用户标识
     * @param roleType 角色
     * @return 商户绑定信息
     * @throws Exception 异常
     */
    @Override
    public List<MchntBindInfo> selectBspsMchntBindInfoByMchntNoAndRoleType(String instId, String mchntNo, String roleType) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBindInfoByMchntNoAndRoleType");
        MchntBindInfo mchntBindInfo = new MchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setMchntNo(mchntNo);
        mchntBindInfo.setBindUserRole(roleType);
        mchntBindInfo.setBindStatus(BindStatusEnum.BIND.getStatus()); //绑定
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBindInfo, MchntBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据用户角色<{}>，商户号：<{}>获取商户绑定信息为空!", roleType, mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBindInfo) (item)).collect(Collectors.toList());
    }

    @Override
    public int insertBspsMchntBindInfo(MchntBindInfo mchntBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertBspsMchntBindInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("插入商户绑定关系表失败,iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public MchntBindInfo selectBspsMchntBindInfoListByUserIdAndMchntNo(String instId, String userId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBindInfoByUserIdAndMchntNo");
        MchntBindInfo mchntBindInfo = new MchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setMchntNo(mchntNo);
        mchntBindInfo.setBindUserId(userId);
        mchntBindInfo.setBindStatus(BindStatusEnum.BIND.getStatus()); //绑定
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBindInfo, mchntBindInfo);
        if (0 != iReturnCode) {
            rglog.error("根据用户Id<{}>，商户号：<{}>获取商户绑定信息为空!", userId, mchntNo);
            return null;
        }
        return mchntBindInfo;
    }

    @Override
    public List<MchntBindInfo> selectBspsMchntBindInfoListByMchntNoAndChainMchntNo(String instId, String userId, String mchntNo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectBspsMchntBindInfoListByMchntNoAndChainMchntNo");
        MchntBindInfo mchntBindInfo = new MchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setMchntNo(mchntNo);
        if (!StringUtil.isEmptyOrNull(userId)) {
            mchntBindInfo.setBindUserId(userId);
        }
        mchntBindInfo.setBindStatus(BindStatusEnum.BIND.getStatus()); //绑定
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntBindInfo, MchntBindInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据用户角色<{}>，商户号：<{}>获取商户绑定信息为空!", mchntNo);
            return null;
        }
        return objectList.stream().map(item -> (MchntBindInfo) (item)).collect(Collectors.toList());
    }

    @Override
    public int updateBspsMchntBindInfoStatusByUserIdAndMchntNo(String instId, MchntBindInfo mchntBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsMchntBindInfoStatusByUserIdAndMchntNo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("解绑收银员失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateBspsMchntBindInfoMsgUserByPrimaryKey(MchntBindInfo mchntBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsMchntBindInfoMsgUserByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("收银员消息设置失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateBspsMchntBindInfo(MchntBindInfo mchntBindInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateBspsMchntBindInfo");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntBindInfo, null);
        if (0 != iReturnCode) {
            rglog.error("修改收银员信息失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateBspsMchntBindInfoStatusByUserId(String instId, String userId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        MchntBindInfo mchntBindInfo = new MchntBindInfo();
        mchntBindInfo.setInstId(instId);
        mchntBindInfo.setBindStatus(BindStatusEnum.UNBIND.getStatus());
        mchntBindInfo.setUnBindTime(DateUtil.getCurrentTime());
        mchntBindInfo.setUnBindDate(DateUtil.getCurrentDate());
        mchntBindInfo.setBindUserId(userId);
        mchntBindInfo.setLastOprId(userId);
        mchntBindInfo.setLastUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));

        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateMchntBindStatus");
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, mchntBindInfo, null);
        if (0 != iReturnCode && BspsAppParamConstant.NODATA_CODE != iReturnCode) {
            rglog.error("更新用户绑定商户状态信息失败,iReturnCode:<{}>", iReturnCode);
        }
        return iReturnCode;
    }
}
